package com.app.ui.pager;

import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.app.ui.activity.base.BaseActivity;
import com.app.ui.activity.base.BaseApplication;
import com.app.ui.dialog.DialogCustomWaiting;
import com.app.ui.view.LoadingLayout;
import com.app.utiles.other.ToastUtile;
import com.retrofits.net.common.RequestBack;
import com.wxj.zx.R;

/**
 * Created by Administrator on 2016/8/15.
 */
public abstract class BaseViewPager implements RequestBack {
    protected BaseActivity baseActivity;
    private boolean isLoadingShow;
    private LoadingLayout loading;
    protected BaseApplication baseApplication;

    public BaseViewPager(BaseActivity baseActivity) {
        this.baseActivity = baseActivity;
        baseApplication = (BaseApplication) baseActivity.getApplication();
    }

    public BaseViewPager(BaseActivity baseActivity, boolean isLoadingShow) {
        this.baseActivity = baseActivity;
        this.isLoadingShow = isLoadingShow;
        baseApplication = (BaseApplication) baseActivity.getApplication();
    }

    protected abstract View onViewCreated();

    /**
     * 加载页面
     **/

    private LoadingLayout loadingView;
    private View rootView;

    public View getView() {
        if (rootView == null) {
            rootView = assemblyView();
        }
        return rootView;
    }

    private View assemblyView() {
        if (rootView != null) {
            return rootView;
        }
        View view = onViewCreated();
        if (!isLoadingShow) {
            return view;
        }
        RelativeLayout relativeView = new RelativeLayout(baseActivity);
        RelativeLayout.LayoutParams contextRl = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(contextRl);
        relativeView.addView(view);
        //加载视图
        loadingView = new LoadingLayout(baseActivity);
        loadingView.setLayoutParams(contextRl);
        relativeView.addView(loadingView);
        loadingView.setOnResetLoagding(new OnCliickReset());
        loadingView.startLoading();
        return relativeView;
    }

    private DialogCustomWaiting dialog;

    protected void dialogShow() {
        if (dialog == null) {
            dialog = new DialogCustomWaiting(baseActivity);
        }
        dialog.show();
    }

    protected void dialogDismiss() {
        if (dialog == null) {
            return;
        }
        dialog.dismiss();
    }

    //=============加载==============
    //重新请求
    public void doRequest() {
    }

    //加载失败
    public void loadingFailed() {
        if (loadingView == null) {
            return;
        }
        loadingView.setLoadingFailed();
    }

    //加载成功
    public void loadingSucceed() {
        if (loadingView == null) {
            isLoadingShow = false;
            return;
        }
        loadingView.setLoadingSucceed();
        loadingView.setVisibility(View.GONE);
    }

    //重新显示加载视图
    public void loadingRest() {
        if (loadingView == null) {
            return;
        }
        loadingView.setVisibility(View.VISIBLE);
        loadingView.startRest();
    }
    protected void loadingSucceed(boolean isEmpty, boolean isEmptyClick) {
        loadingSucceed(isEmpty, R.mipmap.loagding_empty, "什么都没有发现",isEmptyClick);
    }
    /**
     * @param isEmpty      是否显示空数据视图
     * @param isEmptyClick 是否响应点击事件
     * @param iconResId    图片资源id
     * @param content      文本提示
     */
    public void loadingSucceed(boolean isEmpty, int iconResId, String content, boolean isEmptyClick) {
        if (!isEmpty) {
            loadingSucceed();
            return;
        }
        loadingEmptyClick(iconResId, content, isEmptyClick);
    }

    // EmptyView
    public void loadingEmptyClick(int iconResId, String content, boolean isClick) {
        if (loadingView == null) {
            return;
        }
        loadingView.setEmptyContent(iconResId, content, isClick);
        loadingView.setVisibility(View.VISIBLE);
    }

    //重新加载
    class OnCliickReset implements LoadingLayout.OnResetLoagding {
        @Override
        public void onResetRequest() {
            doRequest();
        }
    }

    //======================加载结束=============================
    @Override
    public void OnBack(int what, Object obj, String msg, String other) {

        if (!TextUtils.isEmpty(other)) {
            ToastUtile.showToast(other);
            return;
        }
        if (!TextUtils.isEmpty(msg)) {
            ToastUtile.showToast(msg);
            return;
        }
    }

    public void onData(String... arg0) {
    }

    public void onDestory() {

    }
}
